//
//  YBNewsDetailViewController.m
//  GoodBuy
//
//  Created by Cyber on 15/5/16.
//  Copyright (c) 2015年 GoodBuy. All rights reserved.
//

#import "YBNewsDetailViewController.h"

@interface YBNewsDetailViewController ()

@end

@implementation YBNewsDetailViewController

-(void)dealloc {
    [_newsDetailModel release];
    [_newsListModel release];
    [_titleLabel release];
    [_contentView release];
    [_scrollView release];
    [_footView release];
    [super dealloc];
}
- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _isloadingFinished = NO;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"shoucang"] style:UIBarButtonItemStyleDone target:self action:@selector(collection)];
    
//    NewsCollectHandle *newsCollect = [NewsCollectHandle alloc];
    
    // Do any additional setup after loading the view.
    self.scrollView = [[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    [self.view addSubview:_scrollView];
    
    self.footView =[[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height-50, [UIScreen mainScreen].bounds.size.width, 50)];
//    [[UIView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 50, [UIScreen mainScreen].bounds.size.width, 50)];
    _footView.backgroundColor = [UIColor whiteColor];
    _footView.alpha = 0.9;
    [self.view addSubview:_footView];
    
//    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
//    btn.frame = CGRectMake(_footView.frame.size.width/2.2, 5, 40, 40);
//    //     _btn.backgroundColor = [UIColor yellowColor];
//    [btn addTarget:self action:@selector(QQ) forControlEvents:UIControlEventTouchUpInside];
//    [_footView addSubview:btn];
//    [btn setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeSystem];
    btn1.frame = CGRectMake(_footView.frame.size.width/1.5 + 25, 5, 40, 40);
    //    btn1.backgroundColor = [UIColor greenColor];
    [btn1 addTarget:self action:@selector(Conmment) forControlEvents:UIControlEventTouchUpInside];
    [_footView addSubview:btn1];
    [btn1 setImage:[UIImage imageNamed:@"iconfont-pinglun-2"] forState:UIControlStateNormal];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeSystem];
    btn2.frame = CGRectMake(_footView.frame.size.width/4 - 25, 5, 40, 40);
    //        btn2.backgroundColor = [UIColor greenColor];
    [btn2 addTarget:self action:@selector(Good) forControlEvents:UIControlEventTouchUpInside];
    [_footView addSubview:btn2];
    [btn2 setImage:[UIImage imageNamed:@"iconfont-houtui"] forState:UIControlStateNormal];
    

    
    //菊花旋转
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:_HUD];
    _HUD.labelText = @"亲 请稍等";
    _HUD.tintColor = [UIColor cyanColor];
    [_HUD showAnimated:YES whileExecutingBlock:^{
        //刷新时间最大上限
        //        [self showtime];
    } completionBlock:^{
        
    }];
    
    
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, [UIScreen mainScreen].bounds.size.width - 20, 50)];
    _titleLabel.numberOfLines = 0;
    [_scrollView addSubview:_titleLabel];
    [_titleLabel release];
    
    self.contentView = [[UIWebView alloc]initWithFrame:CGRectMake(0, _titleLabel.frame.size.height + 20, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    [_scrollView addSubview:_contentView];
    [_contentView setScalesPageToFit:NO];
    [_contentView release];
    
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string = [NSString stringWithFormat:@"http://api.smzdm.com/v1/news/articles/%@?f=iphone", _newsListModel.article_id];
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        //        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        NSDictionary *dic1 = [[NSDictionary alloc]init];
        dic1 = responseObject;
        NSMutableDictionary *dic2 = [[NSMutableDictionary alloc]init];
        dic2 = [dic1 objectForKey:@"data"];
        _newsDetailModel = [[YBNewsDetailModel alloc]initWithDic:dic2];
        NSLog(@"%@", _newsDetailModel.article_title);
        _titleLabel.text = _newsDetailModel.article_title;
        [_contentView loadHTMLString:_newsDetailModel.article_filter_content baseURL:nil];
        _contentView.scrollView.bounces = NO;
        _contentView.delegate = self;
        
        NSString *height_str = [_contentView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"];
        int height = [height_str intValue];
        _contentView.frame = CGRectMake(0,_titleLabel.frame.size.height + 20,[UIScreen mainScreen].bounds.size.width,height);
        [_contentView setHidden:YES];
        _scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width,height+130);
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
    
    
}
- (void)Conmment {
    NewsCommentViewController *comment = [[NewsCommentViewController alloc] init];
    comment.news = _newsDetailModel;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController: comment animated:YES];
    self.hidesBottomBarWhenPushed = NO;

}

- (void)Good {
    UIImageView *imageView = [[UIImageView alloc]init];
    NSURL *url = [NSURL URLWithString:self.newsDetailModel.article_pic];
    NSString *str = [NSString stringWithFormat:@"#GoodBuy推荐#我正在看%@,这个新闻,你也来看看吧!%@", self.newsDetailModel.article_title,self.newsDetailModel.article_url];
    [imageView sd_setImageWithURL:url];
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"5551988067e58eb48d00103b"
                                      shareText:str
                                     shareImage:imageView.image
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToEmail,UMShareToFacebook,UMShareToTwitter,UMShareToSms,nil]
                                       delegate:self];
}

- (void)collection {
    [[NewsCollectHandle shareNewsHandle] openMC];
    [[NewsCollectHandle shareNewsHandle] createMC];
    NSArray *array = [[NewsCollectHandle shareNewsHandle] selectAllNews];
    int flag = 0;
    for (YBNewsDetailModel *newsDetail in array) {
        if ([self.newsDetailModel.article_id isEqualToString:newsDetail.article_id]) {
            flag = 1;
            break;
        }
    }
    if (flag) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"已经收藏过了呢." message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
    }else {
        [[NewsCollectHandle shareNewsHandle] insertNews:self.newsDetailModel];
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"收藏成功" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
    }

}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed=YES;
    }
    return self;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    if (_isloadingFinished) {
        [_contentView setHidden:NO];
                return;
    }
    
    NSString *height_str= [webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"];
    int height = [height_str intValue];
    _contentView.frame = CGRectMake(0,_titleLabel.frame.size.height + 20,[UIScreen mainScreen].bounds.size.width,height);
    [_contentView setHidden:NO];
    _scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width,height+130);
    [_HUD removeFromSuperview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
